package primetest.impl.dao;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.BedDto;
import primetest.impl.dao.BedDao;
import primetest.impl.dao.Transaction;

//@Stateless
public class BedDao implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 6551770662358620809L;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private static List<BedDto> allItems = null;
	private static HashMap<String, BedDto> map = null;

	@Inject
	public BedDao() {
	}

	public List<BedDto> getAllList(Transaction transaction)
			throws IOException {
		if (allItems != null) {
			return allItems;
		}
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		map = new HashMap<String, BedDto>();
		try {

			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(GET_ALL);
			resultSet = prepareStatement.executeQuery();

			while (resultSet.next()) {
				BedDto bedDto = makeBedDto(transaction,
						resultSet);
				map.put(bedDto.getKey(), bedDto);
			}
			allItems = new ArrayList<BedDto>(map.values());
			return allItems;
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}
	}

	private BedDto makeBedDto(Transaction transaction,
			ResultSet resultSet) throws SQLException, IOException {
		BedDto bedDto = new BedDto(resultSet.getString(1),
				resultSet.getString(2), resultSet.getString(3), resultSet.getString(4));
		return bedDto;
	}

	public void addBed(Transaction transaction, BedDto bedDto)
			throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		if (map == null) {
			getAllList(transaction);
		}

		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(INSERT_ONE);
			prepareStatement.setString(1, bedDto.getKey());
			prepareStatement.setString(2, bedDto.getName());
			prepareStatement.setString(3, bedDto.getStatus());
			prepareStatement.setString(4, bedDto.getWardKey());
			prepareStatement.executeUpdate();
			map.put(bedDto.getKey(), bedDto);
			allItems.add(bedDto);
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}
	}

	public void updateBed(Transaction transaction, BedDto bedDto)
			throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;

		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(UPDATE_ONE);
			prepareStatement.setString(4, bedDto.getKey());
			prepareStatement.setString(1, bedDto.getName());
			prepareStatement.setString(2, bedDto.getStatus());
			prepareStatement.setString(3, bedDto.getWardKey());
			prepareStatement.executeUpdate();
//			map.put(bedDto.getKey(), bedDto);
//			allItems = new ArrayList<BedDto>(map.values());
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}

	}

	public BedDto findBed(Transaction transaction, String key) throws IOException {
		if(allItems == null){
			getAllList(transaction);
		}
		return map.get(key);
	}

	public void deleteBed(Transaction transaction, String bedSeq)
			throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;

		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(DELETE_ONE);
			prepareStatement.setString(1, bedSeq);
			prepareStatement.executeUpdate();
			allItems.remove(map.remove(bedSeq));
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}

	}

	public void deleteByWard(Transaction transaction, String wardKey, List<BedDto> beds)
			throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;

		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(DELETE_ONE_BY_WARD);
			prepareStatement.setString(1, wardKey);
			prepareStatement.executeUpdate();
			for (BedDto bed : beds) {
				allItems.remove(map.remove(bed.getKey()));
			}
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}

	}

	private static final String GET_ALL = "select key_1, name, status, wardKey from s_bed";
	private static final String INSERT_ONE = "insert into s_bed(key_1, name, status, wardKey) values (?, ?, ?, ?)";
	private static final String UPDATE_ONE = "update s_bed set name = ?, status = ?, wardKey = ? where key_1 = ?";
	private static final String DELETE_ONE = "delete from s_bed where key_1 = ?";
	private static final String DELETE_ONE_BY_WARD = "delete from s_bed where wardKey = ?";
}
